<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               v-model:page="page"
               v-model:search="search"
               :permission="permissionList"
               :before-open="beforeOpen"
               :row-class-name="rowClassName"
               :cell-class-name="rowClassName"
               v-model="form"
               ref="crud"
               @sort-change="sortChange"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button
                type="primary"
                icon="el-icon-plus"
                v-if="permission.purchase_note_main_add"
                @click="handleAdd"
        >新 增
        </el-button>
        <!--<el-button type="danger"-->
                   <!--icon="el-icon-delete"-->
                   <!--v-if="permission.purchase_note_main_delete"-->
                   <!--@click="handleDelete">删 除-->
        <!--</el-button>-->
      </template>
      <template #deliDate-search="{ disabled, size }">
        <!--<div style="display: flex;flex-direction: start;">-->
          <!--<el-select v-model="query.deliDateRule" placeholder="比较" filterable clearable>-->
            <!--<el-option label="" value=""></el-option>-->
            <!--<el-option value=">" label="大于">大于 </el-option>-->
            <!--<el-option value=">=" label="大于等于">大于等于 </el-option>-->
            <!--<el-option value="=" label="等于">等于 </el-option>-->
            <!--<el-option value="!=" label="不等于">不等于 </el-option>-->
            <!--<el-option value="<=" label="小于等于">小于等于 </el-option>-->
            <!--<el-option value="<" label="小于">小于 </el-option>-->
          <!--</el-select>-->
          <!--<el-date-picker-->
                  <!--size="default"-->
                  <!--v-model="query.deliDateSearch"-->
                  <!--type="date"-->
                  <!--:format="dateFormat"-->
                  <!--:value-format="dateFormat"-->
                  <!--:unlink-panels="true"-->
                  <!--clearable-->
                  <!--placeholder="创建日期"-->
                  <!--@change="handleDatePickerChange"-->
                  <!--:picker-options="pickerOptions"-->
          <!--&gt;-->
          <!--</el-date-picker>-->
        <!--</div>-->
        <el-date-picker
                size="default"
                style="width:94%"
                v-model="this.deliDateRange"
                type="daterange"
                :format="dateFormat"
                :value-format="dateFormat"
                unlink-panels
                start-placeholder="Start Date"
                end-placeholder="End Date"
                clearable
                placeholder="送货日期"
                @change="handleDatePickerChange"
                :picker-options="pickerOptions"
        >
        </el-date-picker>
      </template>
      <template #menu="{ row, size, type }">
        <el-tooltip content="查看" placement="top">
          <el-button
                  icon="el-icon-view"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleView(row)"
                  v-if="permission.purchase_note_main_view"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="修改" placement="top">
          <el-button
                  icon="el-icon-edit"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleEdit(row)"
                  v-if="permission.purchase_note_main_edit"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="通知单报表" placement="top">
          <el-button
                  icon="Reading"
                  link
                  :size="size"
                  :type="type"
                  @click="() => showChoice(row)"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button
                  icon="el-icon-delete"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowDel(row)"
                  v-if="permission.purchase_note_main_delete"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
    <purchase-note-form ref="purchaseNoteForm" @success="refreshChange"></purchase-note-form>
  </basic-container>
</template>

<script>
    import {getList, getDetail, add, update, remove,purNoteReport} from "@/api/anwise/purchase/purchase_note_main";
    import {userCompany} from "@/api/anwise/common";
  import {mapGetters} from "vuex";
  import purchaseNoteForm from './components/purchase_note_form.vue';
  export default {
      components: {
          purchaseNoteForm,
      },
    data() {
      return {
        form: {},
        query: {},
          search:{},
          deliDateRange:[],
        loading: true,
          dateFormat: 'YYYY-MM-DD',
          pickerOptions: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
            height:'auto',
            calcHeight: 30,
            tip: false,
            searchShow: false,
            searchMenuSpan: 6,
            border: true,
            index: true,
            selection: true,
            reserveSelection:true,
            labelWidth: 150,
            searchLabelWidth:100,
            indexWidth:100,
            menuWidth: 210,
            addBtn: false,
            editBtn: false,
            viewBtn: false,
            delBtn: false,
            stripe: true,
            searchIndex: 3,
            searchIcon: false,
            searchEnter:true,
          column: [
            {
              label: "通知单编号",
              prop: "idNo",
              type: "number",
              controls:false,
                fixed: true,
                search: true,
                overHidden: true,
                sortable: true,
                width:180,
            },
            {
              label: "供应商编号",
              prop: "supNo",
              type: "number",
              controls:false,
                fixed: true,
                search: true,
                overHidden: true,
                sortable: true,
                width:180,
              rules: [{
                required: true,
                message: "请输入供应商编号",
                trigger: "blur"
              }],
            },
            {
              label: "供应商名称",
              prop: "supName",
              type: "input",
                search: true,
                overHidden: true,
                sortable: true,
              width:180,
              rules: [{
                required: true,
                message: "请输入供应商名称",
                trigger: "blur"
              }],
            },
            {
              label: "发送单位",
              prop: "sendOrg",
              type: "input",
                overHidden: true,
                sortable: true,
                width:150,
              rules: [{
                required: true,
                message: "请输入发送单位",
                trigger: "blur"
              }],
            },
            {
              label: "抄送",
              prop: "ccTo",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "送货日期",
              prop: "deliDate",
              type: "input",
                search: true,
                overHidden: true,
                sortable: true,
                width:150,
              rules: [{
                required: true,
                message: "请输入送货日期",
                trigger: "blur"
              }],
            },
            {
              label: "创建人",
              prop: "createUserName",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "input",
                overHidden: true,
                sortable: true,
                width:150,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.purchase_note_main_add, false),
          viewBtn: this.validData(this.permission.purchase_note_main_view, false),
          delBtn: this.validData(this.permission.purchase_note_main_delete, false),
          editBtn: this.validData(this.permission.purchase_note_main_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
        showChoice(row){
          purNoteReport(row.id, this.isEnglish,this.hiddenBom).then(res => {
            if(res.data.success) {
              const file = res.data.data;
              // const originUrl = (file.url.replace(file.domain, `${this.res}/`));
              const url = file.url;
              // this.$refs.preview.open({
              //     url: url || "",
              //     originUrl: originUrl || ""
              // });
              console.log(url)
              window.open(`/plugins/pdfjs/web/viewer.html?file=`+url);
            }
          })
        },
        handleView(row) {
            let title="查看通知单信息";
            let type='view';
            const id = row.id;
            this.$refs.purchaseNoteForm.show({
                id,
                title,
                type
            })
        },
        handleEdit(row) {
            let title="通知单信息";
            let type='edit';
            const id = row.id;
            this.$refs.purchaseNoteForm.show({
                id,
                title,
                type
            })
        },
        handleAdd() {
            userCompany().then(res => {
                if (res.data.success) {
                    this.dept = res.data.data;
                    if (!this.dept) {
                        this.$message({
                            type: "error",
                            message: "用户无组织机构信息，请联系安伟工作人员！"
                        });
                    } else {
                        let title="通知单信息";
                        let type='edit';
                        this.$refs.purchaseNoteForm.show({
                            title,
                            type
                        })
                    }
                }
            })
        },
        sortChange(val) {
            console.log(val)
            this.sort = {
                prop: val.prop,
                order: val.order
            };
            let order = "";
            if (this.sort.prop && this.sort.order) {
                if (this.sort.order === "descending") {
                    order = `${this.sort.prop} desc`
                } else {
                    order = `${this.sort.prop} asc`
                }
            }
            this.query.orderBy = order;
            this.onLoad(this.page);
        },
        rowSave(row, done, loading) {
            add(row).then(() => {
                this.onLoad(this.page);
                this.$message({
                    type: "success",
                    message: "操作成功!"
                });
                done();
            }, error => {
                loading();
                window.console.log(error);
            });
        },
        rowUpdate(row, index, done, loading) {
            update(row).then(() => {
                this.onLoad(this.page);
                this.$message({
                    type: "success",
                    message: "操作成功!"
                });
                done();
            }, error => {
                loading();
                window.console.log(error);
            });
        },
        rowDel(row) {
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    return remove(row.id);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                });
        },
        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning("请选择至少一条数据");
                return;
            }
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    return remove(this.ids);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    this.$refs.crud.toggleSelection();
                });
        },
        beforeOpen(done, type) {
            if (["edit", "view"].includes(type)) {
                getDetail(this.form.id).then(res => {
                    this.form = res.data.data;
                });
            }
            done();
        },
        searchReset() {
            this.query = {};
            this.search= {};
            this.deliDateRange = [];
            this.search.deliDateStart = '';
            this.search.deliDateEnd = '';
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1
            this.onLoad(this.page, params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        currentChange(currentPage){
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize){
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        onLoad(page, params = {}) {
            this.loading = true;
            getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                const data = res.data.data;
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        },
        handleDatePickerChange(value) {
            console.log(this.search);
            if (value) {
                this.search.deliDateStart = value[0];
                this.search.deliDateEnd = value[1];
            } else {
                this.search.deliDateStart = '';
                this.search.deliDateEnd = '';
            }
        },
    }
  };
</script>

<style>
</style>
